package com.mybatis.mapper;

import com.mybatis.po.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * ClassName: ProductMapper
 * Package: com.mybatis.mapper
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/11/20 19:20
 * @Version 1.0
 */
public interface ProductMapper {

    @SelectKey(keyProperty = "id",keyColumn = "id",resultType = int.class,statement = "SELECT LAST_INSERT_ID()",before = false)
    @Insert("INSERT INTO mybatis03.product(name, price, detail, pic, createtime) VALUES " +
            "(#{name},#{price},#{detail},#{pic},#{createtime})")
    void saveProduct(Product product);

    @Update("UPDATE mybatis03.product SET name = #{name},price = #{price} ,detail = #{detail} ,pic = #{pic} ,createtime = #{createtime} " +
            "WHERE id = #{id} ")
    void updateProduct(Product product);

    @Delete("DELETE FROM mybatis03.product WHERE id = #{id} ")
    void deleteProduct(int id);

    @Select("SELECT * FROM mybatis03.product")
    List<Product> findALlProduct();


    /**
     * //@Results注解相当于映射文件中的<resultMap></resultMap>
     * //@Result注解相当于映射文件中的<result></result>
     * //id属性相当于<id></id>
     * @param id
     * @return
     */
    @Results({
            @Result(column = "pid",property = "id",id = true),
            @Result(column = "pname",property = "name")
    })
    @Select("SELECT id pid, name pname, price, detail, pic, createtime FROM mybatis03.product WHERE id = #{id}")
    Product findById(int id);

}
